package com.framework.core.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 樊猛 [986314249@qq.com]
 * @create 2018/5/25 17:40
 */
public class ServletResponseWriteUtils {



    private static final Logger logger = LoggerFactory.getLogger(ServletResponseWriteUtils.class);

    public static void writeJson(Object object, ServletResponse servletResponse){

        PrintWriter printWriter = null;
        try {
            servletResponse.setContentType("text/plain; charset=utf-8");
            printWriter = servletResponse.getWriter();
            printWriter.print(JSON.toJSON(object));
            printWriter.flush();
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
    }
}
